package com.mytest.demo;

import org.junit.Test;

import java.sql.*;

public class Jdbc {

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf8&useSSL=false","root","hello123");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String name = "tom";
        //String password = "123456";
        String password = "' or '1' = '1";
        String sql = "select * from account where name='"+name+"' and password='"+password+"'";
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            String s = "";
            while (rs.next()){
                final ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                for (int i=1; i<=columnCount; i++) {
                    s = s+metaData.getColumnLabel(i)+":"+rs.getString(i)+" ";
                }
            }
            System.out.println(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void method1(){

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf8&useSSL=false","root","hello123");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String name = "tom";
        //String password = "123456";
        String password = "' or '1' = '1";
        String sql = "select * from account where name = ? and password = ?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,password);
            ResultSet rs = preparedStatement.executeQuery();
            String s = "";
            while (rs.next()){
                final ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                for (int i=1; i<=columnCount; i++) {
                    s = s+metaData.getColumnLabel(i)+":"+rs.getString(i)+" ";
                }
            }
            System.out.println(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void method2(){
        System.out.println("\u001a");
    }

    @Test
    public void method3(){

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/crm_2018?characterEncoding=utf8&useSSL=false","root","hello123");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //String password = "123456";
        String sql = "select version()";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            String s = "";
            while (rs.next()){
                final ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                for (int i=1; i<=columnCount; i++) {
                    s = s+metaData.getColumnLabel(i)+":"+rs.getString(i)+" ";
                }
            }
            System.out.println(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
